<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="文件名称">
              <a-input placeholder="请输入文件名称" v-model="queryParam.docName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="三甲条款">
              <a-input placeholder="请输入三甲条款" v-model="queryParam.jic"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="文档状态">
                <j-dict-select-tag placeholder="请选择文档状态" v-model="queryParam.docStatus" dictCode="doc_status"/>
              </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="是否过期">
                <j-dict-select-tag placeholder="请选择是否过期" v-model="queryParam.docExpired" dictCode="doc_expired"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <template>
      <a-row :gutter="10">
        <a-col :md="4" :sm="24">
          <a-card :bordered="false">
            <div style="background: #fff;padding-left:1px;height: 100%; margin-top: 5px" class="no-left">
              <!-- 树-->
              <template v-if="userIdentity === '2' && categoryTree.length>0">
                <!--组织机构-->
                <a-tree
                  showLine
                  :checkStrictly="true"
                  :treeData="categoryTree"
                  :autoExpandParent="autoExpandParent"
                  @select="onSelect"
                />
              </template>
              <template v-else-if="userIdentity === '2' && categoryTree.length==0">
                <a-empty/>
              </template>
            </div>
          </a-card>
        </a-col>
        <a-col :md="20" :sm="24">
          <a-card :bordered="false">
            <!-- table区域-begin -->
            <div>
              <a-table
                ref="table"
                size="middle"
                bordered
                rowKey="id"
                :columns="columns"
                :dataSource="dataSource"
                :pagination="ipagination"
                :loading="loading"
                @change="handleTableChange">

                <template slot="htmlSlot" slot-scope="text">
                  <div v-html="text"></div>
                </template>
                <template slot="imgSlot" slot-scope="text">
                  <span v-if="!text" style="font-size: 12px;font-style: italic;">无此图片</span>
                  <img v-else :src="getImgView(text)" height="25px" alt="图片不存在"
                       style="max-width:80px;font-size: 12px;font-style: italic;"/>
                </template>
                <template slot="fileSlot" slot-scope="text">
                  <span v-if="!text" style="font-size: 12px;font-style: italic;">无此文件</span>
                  <a-button
                    v-else
                    :ghost="true"
                    type="primary"
                    icon="download"
                    size="small"
                    @click="uploadFile(text)">
                    下载
                  </a-button>
                </template>

                <span slot="action" slot-scope="text, record">
<!--          <a @click="handleInfo(record)">查看</a>-->
                  <a @click="handleYuLan(record)">预览</a>
<!--                  <a-divider type="vertical"/>-->
<!--                <a v-if="record.download==0">-->
<!--                  <a @click="handleDoload(record)">下载</a>-->
<!--                </a>-->
<!--                <a v-if="record.download!=0">-->
<!--                  <a disabled="true">下载</a>-->
<!--                </a>-->
        </span>

              </a-table>
            </div>
          </a-card>
        </a-col>
      </a-row>
    </template>
    <doc-modal-check ref="modalForm" @ok="modalFormOk"></doc-modal-check>
    <EditorInfoReadOnlyModal ref="editInfoForm"/>
  </a-card>
</template>

<script>

  import {JeecgListMixin} from '@/mixins/JeecgListMixin'
  import DocModalCheck from './modules/DocModalCheck'
  import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
  import EditorInfoReadOnlyModal from './modules/EditorInfoReadOnlyModal'
  import {queryCategoryTreeList} from '@/api/api'
  import {getAction, handleExportDoc} from '@/api/manage'
  import {formatDate} from "../../utils/util";

  export default {
    name: 'jianbao',
    mixins: [JeecgListMixin],
    components: {
      JDictSelectTag,
      DocModalCheck,
      EditorInfoReadOnlyModal
    },
    /*created:function() {
      var formData={
        pid:"1243441130173378561"
      }
      getAction(this.url.list, formData, 'get').then((res) => {
        if (res.success) {
          for (let i = 0; i < res.result.records.length; i++) {
            let data = res.result.records[i];
            if (data.docStatus==0){
              this.dataSource.push(data)
            }
          }
        }
      })
    },*/
    data() {
      return {
        id: '',
        selectedKeys: [],
        dataSource: [],
        visible: false,
        hiding: true,
        disableSubmit: false,
        iExpandedKeys: [],
        loading: false,
        autoExpandParent: true,
        disable: true,
        treeData: [],
        categoryTree: [],
        userIdentity: '',
        description: '文档管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function (t, r, index) {
              return parseInt(index) + 1
            }
          },
          {
            title: '文件分类',
            align: 'center',
            dataIndex: 'categoryName'
          },
          {
            title: '文件名称',
            align: 'center',
            dataIndex: 'docName',
            // defaultSortOrder: 'ascend',
            // sorter: (a, b) => a.docName.length - b.docName.length,
          },
          {
            title: '文件编号',
            align: 'center',
            dataIndex: 'docNo',
            // defaultSortOrder: 'ascend',
            // sorter: (a, b) => a.docNo - b.docNo
          },
          {
            title: '定制科室',
            align: 'center',
            dataIndex: 'dzDepartment'
          },
          // {
          //   title: '文件大小',
          //   align: 'center',
          //   dataIndex: 'docSize'
          // },
          {
            title: '三甲条款',
            align: 'center',
            dataIndex: 'jic'
          },
          // {
          //   title: '生效日期',
          //   align: 'center',
          //   dataIndex: 'sxTime',
          //   customRender: function (text) {
          //     return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          //   }
          // },
          // {
          //   title: '到期日期',
          //   align: 'center',
          //   dataIndex: 'dqTime',
          //   customRender: function (text) {
          //     return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          //   }
          // },
          {
            title: '创建日期',
            align: 'center',
            dataIndex: 'createTime',
            sortDirections: ['descend'],
            sortOrder: 'descend',
            sorter: (a, b) =>
            {
              let t1 = a.createTime;
              let t2 = b.createTime;
              t1=t1.replace(/\-/g,'/') // 替换为标准格式 yyyy/mm/dd
              t2=t2.replace(/\-/g,'/')

              var time1 = Date.parse(t1)// 转换为日期格式
              var time2 = Date.parse(t2)

              return time1 < time2 ? false : true
            },
            customRender: function (text) {
              return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
            }
          },
          // {
          //   title: '三甲条款',
          //   align: 'center',
          //   dataIndex: 'isJic_dictText'
          // },
          // {
          //   title: '能否下载',
          //   align: 'center',
          //   dataIndex: 'download_dictText'
          // },
          // {
          //   title: '能否打印',
          //   align: 'center',
          //   dataIndex: 'print_dictText'
          // },
          {
            title: '审核人',
            align: 'center',
            dataIndex: 'audit'
          },
          // {
          //   title: '富文本',
          //   align: "center",
          //   dataIndex: 'editorString'
          // },
          // {
          //   title: '文档状态',
          //   align: 'center',
          //   dataIndex: 'docStatus_dictText'
          // },
          // {
          //   title: '是否过期',
          //   align: 'center',
          //   dataIndex: 'docExpired_dictText'
          // },
          {
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            width: 100,
            scopedSlots: {customRender: 'action'}
          }
        ],
        url: {
          list: '/doc/doc/list',
          delete: '/doc/doc/delete',
          doLoad: "/doc/doc/doLoad",
          deleteBatch: '/doc/doc/deleteBatch',
          exportXlsUrl: '/doc/doc/exportXls',
          importExcelUrl: 'doc/doc/importExcel',
        },
        dictOptions: {},
      }
    },
    computed: {
      importExcelUrl: function () {
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
      }
    },
    methods: {
      handleYuLan(record){
        var param={
          docName:record.docName,
          id:record.id
        }
        getAction(this.url.YuLan, param).then((res) => {
        })
        window.open(window._CONFIG['domianURL']+"/doc/doc/toPdfFile?docName="+record.docName+"&id="+record.id);
      },
      handleDoload(record){
        var param={
          docName:record.docName,
          id:record.id
        }
        getAction(this.url.doLoad, param).then((res) => {
          window.open(window._CONFIG['domianURL']+"/"+res.result.fileKk);
        })

      },
      DownLoadClick(docName, recordId) {
        handleExportDoc(docName, recordId)
      },
      handleInfo: function (record) {
        this.$refs.editInfoForm.edit(record.id)
        this.$refs.editInfoForm.title = '查看'
      },
      initDictConfig() {
      },
      callback(key) {
        console.log(key)
      },
      loadData() {
        this.refresh()
      },
      loadTree() {
        var that = this
        that.treeData = []
        that.categoryTree = []
        queryCategoryTreeList().then((res) => {
          if (res.success && res.result) {
            for (let i = 0; i < res.result.length; i++) {
              let temp = res.result[i]
              that.treeData.push(temp)
              if (temp.children != null && temp.children != 'null' && temp.children.length > 0) {
                for (let j = 0; j < temp.children.length; j++) {
                  let child = temp.children[j]
                  let childElement = child['pid']
                  //没时间了，写死
                  if (childElement == 1243441130173378561) {
                    that.categoryTree.push(child)
                  }
                }
              }
              that.setThisExpandedKeys(temp)
              // console.log(temp.id)
            }
            if (that.categoryTree.length==0){
              var categoryTreeLenth="0"
              if (categoryTreeLenth=="0"){
                this.dataSource = [];
                var formData = {
                  docCategory: "1243441130173378561",
                }
                getAction(this.url.list, formData, 'get').then((res) => {
                  if (res.result.records != null && res.result.records != "null" && res.result.records.length > 0) {
                    for (let i = 0; i < res.result.records.length; i++) {
                      let data = res.result.records[i];
                      if (data.docStatus == "0") {
                        this.dataSource.push(data)
                      }
                    }
                  }
                })
              }
            }
            this.loading = false
          }
          that.userIdentity = res.message
        })
      },
      setThisExpandedKeys(node) {
        if (node.children && node.children.length > 0) {
          this.iExpandedKeys.push(node.key)
          for (let a = 0; a < node.children.length; a++) {
            this.setThisExpandedKeys(node.children[a])
          }
        }
      },
      refresh() {
        this.loading = true
        this.loadTree()
      },
      onSelect(selectedKeys, e) {
        if (this.selectedKeys[0] !== selectedKeys[0]) {
          this.selectedKeys = [selectedKeys[0]]
        }
        let record = e.node.dataRef
        this.dataSource = []
        var formData = {
          docCategory: record.id,
        }
        this.id = record.id
        getAction(this.url.list, formData, 'get').then((res) => {
          if (res.result.records != null && res.result.records != 'null' && res.result.records.length > 0) {
            for (let i = 0; i < res.result.records.length; i++) {
              let data = res.result.records[i]
              if (data.docCategory == this.id && data.docStatus == '0') {
                this.dataSource.push(data)
              }
            }
          }
        })
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>